var queryParam = null;
$(function(){
    var id = $("#caseIdHidden").val();
    queryParam = {"example_id" : id,"pageSize" : 6};
    $.fn.raty.defaults.path = '/by-webhtml/assets/image';
    /* 商品轮播图（带缩略图的轮播效果） */
    showCaseDetail(id);
    showMoney(id);
    showPic(id);
    showTeam(id);
    bindEvent();
});

function bindEvent(){
    $("#userImg").on("click",function(){
        var userId = $("#userId_case").val();
        loadDetailPage(userId);
    });
    $("#userName").on("click",function(){
        var userId = $("#userId_case").val();
        loadDetailPage(userId);
    });
    $('#problem,#showAllMoney').on('click', function(e) { // 点击“有问题，问问我吧”按钮，弹出div
        $.ajax({
            type: 'get',
            url: '/loginWeb/isLogin.json',
            dataType: 'json',
            asycn : false,
            data: {},
            success: function (ret) {
                if (ret.isLogin == 'OK') {


                } else {
                    navPills('loginHtml');
                }
            },
            error: function(e) {
                console.log('error')
            }
        });
        e.stopPropagation();
        $('.desk').fadeIn(100);
        $('.layerDiv').slideDown(200);
        $('#md_mer_mobile_case').val('请输入手机号');
        $('#md_mer_valid_code_case').val('请输入验证码');
    });
    $('#md_mer_popup_div_case .closeDiv').on('click',  function(e) { // 点击“留言弹出层”右上角“X”
        e.stopPropagation();
        $('.desk').fadeOut(100);
        $('.layerDiv').slideUp(200);
    });
    $('#md_mer_get_code_case').on('click',  function (e) { // 点击“获取手机验证码”得按钮得效果
        e.stopPropagation();
        var _mobile = $('#md_mer_mobile_case').val();
        if(!(/^1[34578]\d{9}$/.test(_mobile))){
            alert("请填写正确的手机号码！");
        } else {
            sendValidCode();
        }
    });
    $('#md_mer_vcode_submit_case').on('click',  function(e) {
        e.stopPropagation();
        var _mobile = $('#md_mer_mobile_case').val();
        var _vcode = $('#md_mer_valid_code_case').val();
        if (_mobile && _mobile != '请输入手机号' && _mobile != '') {
            if(!(/^1[34578]\d{9}$/.test(_mobile))){
                alert("请填写正确的手机号码！");
                return;
            } else if (_vcode && _vcode != '请输入验证码' && _vcode != '') {
                $.post('/htmlWeb/valid_code.json', {
                    "mobile": _mobile,
                    "vcode": _vcode,
                    "descn": $('#comments_case').val()
                }, function (wret) {
                    if (wret && wret.retMsg == '成功') {
                        alert('您的反馈我们已收到并及时处理，稍后与您联系，谢谢！')
                        $('#md_mer_popup_div_case, .desk').hide();
                    } else if (wret && wret.retMsg.indexOf('验证码错误') != -1) {
                        alert('验证码错误！');
                    }
                    clearInterval(_ai);
                    $('#md_mer_get_code_case').css({'background-color': '#2caeea', 'cursor': 'pointer'}).val('免费获取验证码');
                    $('#md_mer_get_code_case').on('click',function (e) {
                        e.stopPropagation();
                        sendValidCode();
                    })
                });
            } else {
                alert('请输入验证码！');
            }
        } else {
            alert('请输入手机号码！');
        }
    });
}

var _ai;
function sendValidCode() {
    $('#md_mer_get_code_case').off('click');
    $('#md_mer_get_code_case').css({'background-color': '#ccc', 'cursor': 'default'}).val('60s后可再次发送...');
    var _ai1 = 60;
    _ai = setInterval(function () {
        _ai1 = _ai1 - 1;
        if (_ai1 == 0) {
            clearInterval(_ai);
            $('#md_mer_get_code_case').css({'background-color': '#2caeea', 'cursor': 'pointer'}).val('免费获取验证码');
            $('#md_mer_get_code_case').on('click',  function (e) {
                e.stopPropagation();
                sendValidCode();
            })
        } else {
            $('#md_mer_get_code_case').val(_ai1 + 's后可再次发送...');
        }
    }, 1000);
    var _mobile = $('#md_mer_mobile_case').val();
    if (_mobile && _mobile != '请输入手机号' && _mobile != '') {
        $.post('/registerWeb/sendValidMsgWeb.json', {"mobile": _mobile}, function (ret) {
        });
    } else {
        alert('请输入手机号码！');
    }
}
function showCaseDetail(id){
    $.ajax({
        type : "post",
        url : "htmlWeb/getExamplePageList",//,"payStatus" : -2
        data: {"id" : id,"viewFlag" : "viewCount"},
        async: false,
        success : function(data){
            var str = "";
            $.each(data.rows,function(i,obj){
                $("#userId_case").val(obj.userInfoBvo.id);
                $("#userImg").attr("src","http://img.boyitongcheng.com/" +obj.userInfoBvo.bizCover);
                $("#userNameTitle").html(obj.userInfoBvo.cnName);
                $("#userName").html(obj.userInfoBvo.bizWork.name + ":" + obj.userInfoBvo.cnName);
                $("#wedTime").html("婚礼时间:" + obj.caseTime);
                $("#wedAddr").html("婚礼地点:" + obj.caseHotel);
                $("#wedDetail").html("婚礼描述:" + obj.caseComment);
            });
        },
        error : function(data){
            console.log(data);
        }
    });
}

function showPic(id){
    $.ajax({
        type : "post",
        url : "htmlWeb/getMyExampleDetailList",//,"payStatus" : -2
        data: {"example_id" : id,"pageSize" : 100000},
        async: false,
        success : function(data){
            var picStr = "";
            $.each(data.rows,function(i,obj){
                obj = trimEmpty(obj);
                if(i == 0){
                    picStr += '<li class="on"><img src="http://img.boyitongcheng.com/' + obj.imgUrl +'" /><div class="bun_bg"></div></li>';
                }else{
                    picStr += '<li><img src="http://img.boyitongcheng.com/' + obj.imgUrl +'" /><div class="bun_bg"></div></li>';
                }
            });
            $("#largePic").html(picStr);
            $("#smallPic").html(picStr);
            $(".thum").thumbnailImg({
                large_elem: ".large_box",
                small_elem: ".small_list",
                left_btn: ".left_btn",
                right_btn: ".right_btn"
            });
        },
        error : function(data){
            console.log(data);
        }
    });
}

function showBizInfo(userId) {
    loadDetailPage(userId);
}

function showTeam(id){
    $.ajax({
        type : "post",
        url : "htmlWeb/getRecommendUserList",//,"payStatus" : -2
        data: {"example_id" : id,"pageSize" : 100000},
        async: false,
        success : function(data){
            var teamStr = "";
            $.each(data.rows,function(i,obj){
                obj = trimEmpty(obj);
                if(obj.recommendUser){
                    if(data.rows.length < 3){
                        teamStr += '<li><div class="teamLi"><img onclick="showBizInfo(' + obj.userId + ')" style="cursor: pointer" src="http://img.boyitongcheng.com/' + obj.recommendUser.avatar +'"  /><div class="teamTxt">' +
                            '<a href="#" style="cursor: pointer" onclick="showBizInfo(' + obj.userId + ')">' + obj.recommendUser.bizWork.name + '：' + obj.recommendUser.cnName + '</a>' +
                            '<h5>¥ ' + obj.recommendUser.bizPrice + '</h5><h6>' + obj.perD + '<em>|</em>' + obj.recommendUser.bizDealCount + '预订量</h6></div></div></li>';
                    }else{
                        teamStr += '<li '+ (i == 0 || i % 4 == 3 ? 'style="margin-right: 0px; float: right;"' : '') +'><div class="teamLi"><img onclick="showBizInfo(' + obj.userId + ')" style="cursor: pointer" src="http://img.boyitongcheng.com/' + obj.recommendUser.avatar +'"  /><div class="teamTxt">' +
                            '<a href="#" style="cursor: pointer" onclick="showBizInfo(' + obj.userId + ')">' + obj.recommendUser.bizWork.name + '：' + obj.recommendUser.cnName + '</a>' +
                            '<h5>¥ ' + obj.recommendUser.bizPrice + '</h5><h6>' + obj.perD + '<em>|</em>' + obj.recommendUser.bizDealCount + '预订量</h6></div></div></li>';
                    }
                }
            });
            $("#caseTeam").html(teamStr);
        },
        error : function(data){
            console.log(data);
        }
    });
}

function showMoney(id,pageSize) {
    if(!pageSize){
        pageSize = 100000;
    }
    $.ajax({
        type : "post",
        url : "htmlWeb/getExampleBudgetListByPage",
        data: {"example_id" : id,"pageSize" : pageSize},
        async: false,
        success : function(data){
            $("#caseCostPageHead ~ tr").remove();
            var str = "";
            $.each(data.rows,function(i,obj){
                obj = trimEmpty(obj);
                str += '<tr><td height="50"><p>' + obj.id + '</p></td>' +
                    '<td><span>' + obj.name + '</span></td>' +
                    '<td><span>' + obj.descn + '</span></td>' +
                    '<td><span>' + obj.level + '</span></td>' +
                    '<td><span>' + obj.material + '</span></td>' +
                    '<td><span>' + obj.price + '</span></td>' +
                    '<td><span>' + obj.num + '</span></td>' +
                    '<td><span>' + obj.unit + '</span></td>' +
                    '<td><span>' + obj.money + '</span></td></tr>';
            });
            $("#caseCostPageHead").after(str);
            // if(pageSize == 5){
            //     getListPage(id);
            // }
        },
        error : function(data){
            console.log(data);
        }
    });
}

function showAllMoney() {
    var id = $("#caseIdHidden").val();
    showMoney(id,100000);
}

function getListPage(param){
    var id = $("#caseIdHidden").val();
    param = {"example_id" : id,"pageSize" : 6};
    $.ajax({
        type : "post",
        url : "htmlWeb/getExampleDiscussListByPage",
        data: param,
        async: false,
        success : function(data){
            $("#caseDiscussPageHead ~ tr").remove();
            var str = "",scoreArr = [];
            $.each(data.rows,function(i,obj){
                obj = trimEmpty(obj);
                var createTime = formatDate(obj.createTime,"yyyy-MM-dd");
                str += '<li><a href="#"><img src="http://img.boyitongcheng.com/' + obj.imgUrl + '" width="56px" height="56px"/><span>' + obj.name + '</span></a><div class="raTxt">' +
                    '<h2><em>评分星级：</em><div class="halfShow-true-demo" id="score' + i +'"></div></h2>' +
                    '<p>' + obj.content + '</p>' +
                    '<h3>' + createTime + '</h3></div></li>';
                scoreArr.push(obj.score);
            });
            $("#caseDiscussPageHead").html(str);
            $.each(scoreArr,function(i,data){
                $('#score' + i).raty({readOnly: true, score: data });
            });
            createPage(data);

        },
        error : function(data){
            console.log(data);
        }
    });
}